package com.thinkcube.edutainment.fragment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.widget.ListView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.MyCourseAdapter;
import com.thinkcube.edutainment.client.CourseClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.Course;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.domain.dto.UserOAthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.mycourse)
public class MyCourse extends Fragment {

	@ViewById(R.id.mycourselist)
	ListView myCourseList;

	@RestService
	CourseClient courseClient;

	@Bean
	CourseDto courseDto;

	@RestService
	UserClient userClient;

	@Bean
	OAuthDto oAuthDto;

	@Trace
	@AfterViews
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);
		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setTokenUi(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	void setTokenUi(OAuth requestToken) {

		doBackground(requestToken);
	}

	UserOAthDto userOAthDto = new UserOAthDto();

	@Pref
	SPreferences_ pref;

	@Background
	void doBackground(OAuth requestToken) {
		System.out.println("token " + requestToken.getoAuth().getToken());
		userOAthDto.setAuthToken(requestToken.getoAuth().getToken());
		userOAthDto.setUserId(pref.getId().get());

		doUi(courseClient.getGetMyCourses(userOAthDto.getMultiValuedMap()));

	}

	@Bean
	MyCourseAdapter myCourseAdapter;

	@Trace
	@UiThread
	void doUi(Course course) {
		List<CourseData> courseList = course.getCourseList();

		myCourseAdapter.setCourseDataList(courseList);
		myCourseList.setAdapter(myCourseAdapter);

	}

	public boolean isResolvable(String hostname) {
		try {
			InetAddress.getByName(hostname);
			return true;
		} catch (UnknownHostException e) {
			return false;
		}
	}

	@UiThread
	void showDialog() {
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:

					break;

				case DialogInterface.BUTTON_NEGATIVE:

					break;
				}
			}
		};
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage("Not reachable")
				.setPositiveButton("Yes", dialogClickListener)
				.setNegativeButton("No", dialogClickListener).show();
	}

}
